package com.framework.controller.user;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.framework.dto.PageRequestDTO;
import com.framework.dto.UpdateUserDTO;
import com.framework.vo.UserInfoVO;
import com.framework.entity.User;
import com.framework.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;
import java.util.UUID;

/**
 * @Description: 用户操作相关接口
 * @Author: JamesYao
 * @DateTime: 2023/12/19 23:44
 */
@RestController
@RequestMapping("/user/")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 获取用户信息
     */
    @GetMapping("info")
    public SaResult getUserInfo(){
        return userService.getUserInfo();
    }

    @PostMapping("allUser")
    public SaResult getAllUser(@RequestBody PageRequestDTO pageRequestDTO){
        return userService.getAllUser(pageRequestDTO);
    }

    /**
     * 新增用户
     */
    @PostMapping("addUser")
    public SaResult register(@RequestBody UpdateUserDTO userDTO){
        return userService.saveUser(userDTO);
    }

    @PutMapping()
    public SaResult update(@RequestBody UpdateUserDTO userDTO){
        return userService.updateUser(userDTO);
    }

    /**
     * 删除用户
     * @param userId 用户ID
     * @return 删除结果
     */
    @DeleteMapping("{userId}")
    public SaResult delete(@PathVariable("userId") UUID userId){
        // 如果用户ID为空，则返回传递的用户ID为空的错误信息
        if (Objects.isNull(userId)){
            return SaResult.error("传递的用户ID为空");
        }
        // 调用用户服务删除用户
        return userService.deleteUser(userId);
    }

}
